# Maintainer: Olliver Schinagl <oliver@schinagl.nl>
pkgname=mtd-utils
pkgver=2.2.1
pkgrel=0
pkgdesc="Utilities for handling MTD devices, and for dealing with FTL, NFTL JFFS2, etc."
options="!check" # Tests fail on builder
url="http://www.linux-mtd.infradead.org/"
arch="all"
license="GPL-2.0-only"
makedepends="
	acl-dev
	autoconf
	automake
	coreutils
	cmocka-dev
	libtool
	linux-headers
	lzo-dev
	openssl-dev>3
	util-linux-dev
	zlib-dev
	zstd-dev
	"
checkdepends="findutils"
_subpackages="
	$pkgname-flash
	$pkgname-jffs
	$pkgname-misc
	$pkgname-nand
	$pkgname-nor
	$pkgname-ubi
	$pkgname-dev
	"
subpackages="$pkgname-doc $_subpackages"
source="https://infraroot.at/pub/mtd/mtd-utils-$pkgver.tar.bz2"

build() {
	./configure \
	            --build="$CBUILD" \
	            --enable-tests \
	            --enable-unit-tests \
	            --host="$CHOST" \
	            --mandir="/usr/share/man" \
	            --prefix="/usr" \
	            --sysconfdir="/etc"
	make
}

_cat_found_file() {
	local _rc="$?"
	find -name "$1" -type f -ls -printf '%P {{{\n' -exec cat -v -n '{}' ';' -printf '}}} %P\n'
	return "$_rc"
}

check() {
	make check || _cat_found_file 'test-suite.log'
}

dev() {
	mkdir -p "$subpkgdir/usr/include" "$subpkgdir/usr/lib"
	cd "$builddir/include"
	mv libmtd.h libscan.h libubi.h libubigen.h "$subpkgdir/usr/include/"
	cd $builddir
	mv libmtd.a libscan.a libubi.a libubigen.a "$subpkgdir/usr/lib/"
}

flash() {
	pkgdesc="MTD utils (flash commands)"

	mkdir -p "$subpkgdir/usr/sbin"
	find "$pkgdir/usr/sbin/" -type f \( \
	     -iname '*flash*' \) \
	     -exec mv -t "$subpkgdir/usr/sbin/" "{}" +
}

jffs() {
	pkgdesc="MTD utils (JFFS* commands)"

	echo "$subpkgdir"
	mkdir -p "$subpkgdir/usr/sbin"
	find "$pkgdir/usr/sbin/" -type f \(\
	     -iname '*jffs*' -o -iname 'sumtool' \)\
	     -exec mv -t "$subpkgdir/usr/sbin/" "{}" +
}

misc() {
	pkgdesc="MTD utils (miscellaneous commands)"

	mkdir -p "$subpkgdir/usr/sbin"
	find "$pkgdir/usr/sbin/" -type f \(\
	     -iname '*doc*' -o -iname 'ftl*' -o \
	     -iname 'mtdpart' -o -iname 'mtd_debug' -o \
	     -iname 'recv_image' -o -iname 'serve_image' \)\
	     -exec mv -t "$subpkgdir/usr/sbin/" "{}" +
}

nand() {
	pkgdesc="MTD utils (NAND and FTL commands)"

	mkdir -p "$subpkgdir/usr/sbin"
	find "$pkgdir/usr/sbin/" -type f \(\
	     -iname 'nand*' -o -iname 'nftl*' \)\
	     -exec mv -t "$subpkgdir/usr/sbin/" "{}" +
}

nor() {
	pkgdesc="MTD utils (NOR flash commands)"

	mkdir -p "$subpkgdir/usr/sbin"
	find "$pkgdir/usr/sbin/" -type f \(\
	     -iname '*nor*' -o -iname 'rfd*' \)\
	     -exec mv -t "$subpkgdir/usr/sbin/" "{}" +
}

ubi() {
	pkgdesc="MTD utils (UBI and UBIFS commands)"

	mkdir -p "$subpkgdir/usr/sbin"
	find "$pkgdir/usr/sbin/" -type f \(\
	     -iname '*ubi*' -o \
	     -iname 'lsmtd' -o -iname 'mtdinfo' \)\
	     -exec mv -t "$subpkgdir/usr/sbin/" "{}" +
}

package() {
	depends="$_subpackages"

	make DESTDIR="$pkgdir" install
}

sha512sums="
ef1cf4d6fa5219b8bbe4728282d0d73fd3234c36743cdfb901908269632f55a7993fdbeb22cc2b6841add1a9f320c4504a42980f5856626e259401e8e2f17f7d  mtd-utils-2.2.1.tar.bz2
"
